﻿using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using TTGameEngine;
using TTGame;

/*
 * AudioExtensions
 *
 *
 *
 * 依赖：
 *
 * ！不写重复代码！
 * @author TTun
 * @date 9/5/2017 3:00:56 PM
 */

public static class AudioExtensions {
    public const float DefaultMinDistance = 2.0f;

    #region Methods
    //全部玩家都能听见的声音
    public static void PlayGlobalClip(string aClipName) {
        AudioManager.Ins.PlayEffect(aClipName, AudioManager.Ins.AudioListenerPos, 1, 1, DefaultMinDistance);
    }

    public static void PlayClip(string aClipName, Vector3 aPos, float aVolume = 1, float aPitch = 1, float aMinDistance = DefaultMinDistance, bool fromUSS = false) {
        AudioManager.Ins.PlayEffect(aClipName, aPos, aVolume, aPitch, aMinDistance, fromUSS);
    }
    public static void PlayClip(string aClipName, Transform aParent, float aVolume = 1, float aPitch = 1, float aMinDistance = DefaultMinDistance, bool fromUSS = false) {
        AudioManager.Ins.PlayEffect(aClipName, aParent, aVolume, aPitch, aMinDistance, fromUSS);
    }

    /// <summary>
    /// playtime > 0, 将会自动在播放时间后停止。
    /// playtime = 0，声音将会一直循环，根据返回的Key，由外部控制停止。
    /// </summary>
    public static uint PlayLoopClip(string aClipName, Vector3 aPos, float playtime, float aVolume = 1, float aPitch = 1, float aMinDistance = DefaultMinDistance, bool fromUSS = false) {
        if(playtime > 0) {
            //播放时间到了后自动停止
            return AudioManager.Ins.PlayLoopAudioWithTime(aClipName, aPos, playtime, aVolume, aPitch, aMinDistance, fromUSS);
        } else {
            //返回AudioKey，外部控制循环音效的停止
            return AudioExtensions.StartAudioCustom(aClipName, aPos, true, aVolume, aPitch, aMinDistance);
        }
    }

    public static uint PlayLoopClip(string aClipName, Transform aParent, float playtime, float aVolume = 1, float aPitch = 1, float aMinDistance = DefaultMinDistance, bool fromUSS = false) {
        if(playtime > 0) {
            return AudioManager.Ins.PlayLoopAudioWithTime(aClipName, aParent, playtime, aVolume, aPitch, aMinDistance, fromUSS);
        } else {
            return AudioExtensions.StartAudioCustom(aClipName, aParent, true, aVolume, aPitch, aMinDistance);
        }
    }

    public static void PlayClipRandom(string[] aClipNames, Transform aParent, float aVolume = 1, float aPitch = 1, float aMinDistance = DefaultMinDistance) {
        if(aClipNames != null && aClipNames.Length > 0) {
            PlayClip(aClipNames.RandomValue(), aParent, aVolume, aPitch);
        }
    }
    public static void PlayClipRandom(string[] aClipNames, Vector3 aPos, float aVolume = 1, float aPitch = 1, float aMinDistance = DefaultMinDistance) {
        if(aClipNames != null && aClipNames.Length > 0) {
            PlayClip(aClipNames.RandomValue(), aPos, aVolume, aPitch);
        }
    }
    public static void SetLoopVolume(this AudioSource aAudioSource, float aVolume = 1, float aPitch = 1) {
        aAudioSource.volume = AudioManager.Ins.EffectVolume * aVolume;
    }

/// <summary>
/// 用户自行控制音效关闭时机。
///
/// 可用于自行控制播放和暂停循环音效。
///
/// 注：需要提前加载，保证clip资源已存在，否则返回为null。
/// </summary>
/// <param name="aClipName"></param>
/// <param name="pos"></param>
/// <param name="aVolume"></param>
/// <param name="aPitch"></param>
/// <param name="aMinDistance"></param>
/// <returns>AudioSource的Key值，用此Key值可以控制AudioSource的关闭</returns>
    public static uint StartAudioCustom(string aClipName, Vector3 pos, bool aLoop = true, float aVolume = 1, float aPitch = 1, float aMinDistance = DefaultMinDistance) {
        if(string.IsNullOrEmpty(aClipName)/* || BattleManager.Share().IsBattlePause*/) {
            return CountID_UInt.UNINIT_VALUE;
        }
        return AudioManager.Ins.PlayerEffectCustom(aClipName, pos, aVolume, aPitch, aMinDistance, aLoop);
    }
/// <summary>
/// 用户自行控制音效关闭时机。
///
/// 注：需要提前加载，保证clip资源已存在，否则返回为null。
/// </summary>
/// <param name="aClipName"></param>
/// <param name="parent"></param>
/// <param name="aVolume"></param>
/// <param name="aPitch"></param>
/// <param name="aMinDistance"></param>
/// <returns>AudioSource的Key值，用此Key值可以控制AudioSource的关闭</returns>
    public static uint StartAudioCustom(string aClipName, Transform parent, bool aLoop = true, float aVolume = 1, float aPitch = 1, float aMinDistance = DefaultMinDistance, bool fromUSS = false) {
        if(string.IsNullOrEmpty(aClipName) /*|| BattleManager.Share().IsBattlePause*/) {
            return CountID_UInt.UNINIT_VALUE;
        }
        return AudioManager.Ins.PlayerEffectCustom(aClipName, parent, aVolume, aPitch, aMinDistance, aLoop, fromUSS);
    }
/// <summary>
/// 调用该方法回收由StartAudioLoop创建处理的声音源。
///
/// 注：停止声音源后，原来的引用注意设置null。
/// </summary>
/// <param name="aAudioSource">必须是由StartAudioLoop返回的对象</param>
    public static void StopAudioCustom(uint audioKey) {
        if(audioKey == CountID_UInt.UNINIT_VALUE) {
            return;
        }
        AudioManager.Ins.StopEffectCustom(audioKey);
    }

//用户自己创建的声音源。
    public static void StartEx(this AudioSource aAudioSource, float aVolume = 1) {
        aAudioSource.volume = AudioManager.Ins.EffectVolume * aVolume;
        if(!aAudioSource.isPlaying) { aAudioSource.Play(); }
    }

//自己创建的声音源，根据情况加入管理。用于统一暂停声音。
    public static void RegistAudioToManager(this AudioSource aAudioSource) {
        AudioManager.Ins.userAudioSources.Remove(aAudioSource);
    }
    #endregion Methods
}